<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="规格名称" prop="skuName">
		 <el-input
		   v-model="queryParams.skuName"
		   placeholder="请输入规格名称"
		   clearable
		   @keyup.enter="handleQuery"
		   class="!w-240px"
		 />
	  </el-form-item>
      <el-form-item label="一级分类" prop="categoryOneId">
        <el-select
          v-model="queryParams.categoryOneId"
          placeholder="请选择一级分类"
          clearable
          class="!w-240px" @change="changeCategoryOne"
        >
          <el-option v-for="(item,index) in categoryOneList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" prop="categoryTwoId">
        <el-select
          v-model="queryParams.categoryTwoId"
          placeholder="请选择二级分类"
          clearable
          class="!w-240px"
        >
          <el-option v-for="(item,index) in categoryTwoList" :key="index" :label="item.name" value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存盘点" prop="checkStockStatus">
        <el-select
          v-model="queryParams.checkStockStatus"
          placeholder="请选择库存盘点"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CHECK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否删除" prop="deleted">
        <el-select
          v-model="queryParams.deleted"
          placeholder="请选择是否删除"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺ID" prop="teamId">
        <el-select
          v-model="queryParams.teamId"
          placeholder="请选择店铺ID"
          clearable
          class="!w-240px"
        >
          <el-option v-for="(item,index) in teamList" :key="index" :label="item.shopName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.ON_OFF_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['team:product:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
		<el-button
		  plain
		  type="warning"
		  @click="openPriceForm('update', queryParams)"
		  v-hasPermi="['team:product:price']"
		>
		 <Icon icon="ep:edit" class="mr-5px" /> 批量改价
		</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['team:product:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
		<el-button
		  type="success"
		  plain
		  @click="dialogTableVisible=true"
		  v-hasPermi="['team:product:price']"
		>
		  <Icon icon="ep:upload" class="mr-5px" /> EXCEL导入-批量改价
		</el-button>
		
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="线上商品ID" align="center" prop="productId" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="规格名称" align="center" prop="skuName" />
      <el-table-column label="销售价格" align="center" prop="salePrice" />
      <el-table-column label="成本价" align="center" prop="costPrice" />
      <el-table-column label="商品库存" align="center" prop="stock" />
      <el-table-column label="二级分类名称" align="center" prop="categoryTwoName" />
      <el-table-column label="一级分类名称" align="center" prop="categoryOneName" />
      <el-table-column label="库存盘点" align="center" prop="checkStockStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CHECK_STATUS" :value="scope.row.checkStockStatus" />
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="会员价" align="center" prop="vipPrice" />
      <el-table-column label="店铺ID" align="center" prop="teamId" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ON_OFF_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['team:product:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['team:product:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
	<el-dialog v-model="dialogTableVisible" title="上传价格文件" width="800" center>
	  <div>
		  <el-upload
		      ref="uploadRef"
		      class="upload-demo"
		      :action=uploadFileUrl
			  :headers="headers"
		      :auto-upload="false"
			  :limit="1"
		    >
		      <template #trigger>
		        <el-button type="primary">选择文件</el-button>
		      </template>
		  
		      <el-button class="ml-3" type="success" @click="submitUpload">
		        上传文件
		      </el-button>
		  
		      <template #tip>
		        <div class="el-upload__tip">
		          .xls文件或.xlsx文件,文件不通超过5M
		        </div>
		      </template>
		    </el-upload>
	  </div>
  </el-dialog>
  <!-- 表单弹窗：添加/修改 -->
  <ProductForm ref="formRef" @success="getList" />
  <!-- 表单弹窗：批量改价 -->
  <EditPriceForm ref="priceFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { ref } from 'vue' 
import { getAccessToken } from "@/utils/auth.ts"
import { UploadFilled } from '@element-plus/icons-vue'
import { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { ProductApi, ProductVO } from '@/api/team/teamproduct'
import { ProductCategoryApi } from '@/api/team/productcategory'
import { MemberApi } from '@/api/team/teammember'
import ProductForm from './ProductForm.vue'
import EditPriceForm from './EditPriceForm.vue'
import type { UploadInstance } from 'element-plus'

const uploadRef = ref<UploadInstance>()

const submitUpload = () => {
  uploadRef.value!.submit()
}
 // 上传文件服务器地址
//const uploadFileUrl= ref(process.env.VUE_APP_BASE_API) + "/team/product/import-excel-price";
const uploadFileUrl="http://localhost:48080/admin-api/team/product/import-excel-price";
const headers= {Authorization: "Bearer " + getAccessToken()}
/** 店铺商品库存 列表 */
defineOptions({ name: 'TeamProduct' })
const dialogTableVisible = ref(false)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProductVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  skuName: undefined,
  categoryOneId: undefined,
  categoryTwoId: undefined,
  checkStockStatus: undefined,
  deleted: undefined,
  teamId: undefined,
  status: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductApi.getProductPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 批量改价操作 */
const priceFormRef = ref()
const openPriceForm = (type: string, params: object) => {
  priceFormRef.value.open(type, params)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductApi.deleteProduct(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProductApi.exportProduct(queryParams)
    download.excel(data, '店铺商品库存.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const categoryOneList=ref()
const getCategoryOneList=async () =>{
	const data = await ProductCategoryApi.getProductCategoryList({pid:0});
	categoryOneList.value=data;
}
/* 切换一级分类 */
const categoryTwoList=ref()
const changeCategoryOne = async (id) => {
	const data = await ProductCategoryApi.getProductCategoryList({pid:id});
	categoryTwoList.value=data;
}
/* 店铺 */
const teamList=ref()
const getTeamList = async () => {
	const data = await MemberApi.getShopList();
	teamList.value=data;
}
/** 初始化 **/
onMounted(() => {
  getList()
  getCategoryOneList()
  getTeamList()
})
</script>